package com.yunao.web.convert;

import com.yunao.entity.User;
import com.yunao.service.report.DateUtil;
import com.yunao.web.form.UserForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

public class UserForm2UserConverter {
    public static User convert(UserForm userForm){
        User user = new User();
        user.setUsername(userForm.getName());
        user.setPassword(DigestUtils.md5DigestAsHex(userForm.getPassword().getBytes()));
        user.setMobile(userForm.getPhone());
        if (StringUtils.isNotEmpty(userForm.getEmail()))
            user.setEmail(userForm.getEmail());
        if (StringUtils.isEmpty(userForm.getCity())){
            user.setLocaltion(userForm.getProvince());
        }else {
            user.setLocaltion(userForm.getProvince()+":"+userForm.getCity());
        }
//        user.setLocaltion(userForm.getAddress());
        user.setExpireDate(DateUtil.getDate(userForm.getExpireDate()));
        user.setRole(Integer.parseInt(userForm.getRole()));
        if (StringUtils.isEmpty(userForm.getSecondtrade())){
            user.setTrade(userForm.getFirsttrade());
        }else {
            user.setTrade(userForm.getFirsttrade()+":"+userForm.getSecondtrade());
        }
        if (StringUtils.isNotEmpty(userForm.getOrgan_id())){
            user.setOrganId(Integer.parseInt(userForm.getOrgan_id()));
        }
        user.setSubjectNum(0);
        return user;
    }

    public static User convert(UserForm userForm,User user){
        user.setUsername(userForm.getName());
//        user.setPassword(DigestUtils.md5DigestAsHex(userForm.getPassword().getBytes()));
        user.setMobile(userForm.getPhone());
        if (StringUtils.isNotEmpty(userForm.getEmail()))
            user.setEmail(userForm.getEmail());
        if (StringUtils.isEmpty(userForm.getCity())){
            user.setLocaltion(userForm.getProvince());
        }else {
            user.setLocaltion(userForm.getProvince()+":"+userForm.getCity());
        }
        user.setExpireDate(DateUtil.getDate(userForm.getExpireDate()));
//        user.setLocaltion(userForm.getAddress());
        user.setRole(Integer.parseInt(userForm.getRole()));
        if (StringUtils.isEmpty(userForm.getSecondtrade())){
            user.setTrade(userForm.getFirsttrade());
        }else {
            user.setTrade(userForm.getFirsttrade()+":"+userForm.getSecondtrade());
        }
//        user.setOrganId(Integer.parseInt(userForm.getOrgan_id()));
//        user.setSubjectNum(0);
        return user;
    }
}
